package com.serecttask.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.serecttask.R;
import com.serecttask.model.JsonModel;


import java.util.HashMap;

import util.KsoapUtil;
import util.PostCallback;
import util.StringUtil;
import util.ToastUtils;
import widget.Boolean_Mobile;
import widget.ClearEditText;
import widget.Timer;

//import com.jmhz.ailou.ilou.model.JsonModel;
//import com.jmhz.ailou.ilou.widget.Boolean_Mobile;



/**
 * Created by xiaozhi on 2016/3/10.
 */
public class ForgetActivity extends BaseActivity implements View.OnClickListener {
    private ClearEditText et_forget_phonenumber = null;
    private ClearEditText et_forget_phonecode = null;
    private ClearEditText et_forget_password = null;
    private ClearEditText et_forget_repassword = null;
    private Button btn_forgetPassword_code;
    private Button btn_forgetPassword_submit;



    private String phoneNumber;
    private String phoneNumberLatest;//输入框最后一次的电话号码
    private String verificationCode;
    private String verificationCodeReceive;
    private String newPassword;
    private String newPasswordConfirm;
    private Timer time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        initview();
        initWidget();
    }

    private void initWidget() {
        btn_forgetPassword_code.setOnClickListener(this);
        btn_forgetPassword_submit.setOnClickListener(this);
    }

    private void initview() {
        initTopBar("忘记密码");
        et_forget_phonenumber = (ClearEditText) findViewById(R.id.et_forget_phonenumber);
        et_forget_phonecode = (ClearEditText) findViewById(R.id.et_forget_phonecode);
        et_forget_password = (ClearEditText) findViewById(R.id.et_forget_password);
        et_forget_repassword = (ClearEditText) findViewById(R.id.et_forget_repassword);
        btn_forgetPassword_code = (Button) findViewById(R.id.btn_forgetPassword_code);
        btn_forgetPassword_submit = (Button) findViewById(R.id.btn_forgetPassword_submit);
        time = new Timer(60000, 1000,btn_forgetPassword_code);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_forgetPassword_code:
                //获取本机号码并下发验证码！
                phoneNumber = et_forget_phonenumber.getText().toString();
                if (StringUtil.isEmpty(phoneNumber)) {
                    ToastUtils.show(ForgetActivity.this, "请输入手机号", 1);
                } else if(Boolean_Mobile.isMobileNO(phoneNumber)){
                    time.start();// 开始计时
                    sendVerificationCode();
                } else {
                    ToastUtils.show(ForgetActivity.this, "请重新手机号", 1);
                }
                break;
            case R.id.btn_forgetPassword_submit:
                forgetPassword();
                break;
            case R.id.ll_top_Back:
                finish();
                break;
            default:
                break;
        }
    }

    private void forgetPassword() {
        phoneNumberLatest = et_forget_phonenumber.getText().toString();
        verificationCode = et_forget_phonecode.getText().toString();
        newPassword = et_forget_password.getText().toString();
        newPasswordConfirm = et_forget_repassword.getText().toString();

        if (StringUtil.isEmpty(phoneNumberLatest)) {
            ToastUtils.show(ForgetActivity.this,"手机号不能为空",1);
        } else if (StringUtil.isNullOrEmpty(verificationCode)){
            ToastUtils.show(ForgetActivity.this,"验证码不能为空",1);
        } else if (!Boolean_Mobile.isMobileNO(phoneNumberLatest)) {
            ToastUtils.show(ForgetActivity.this,"请输入正确的手机号",1);
        } else if (!verificationCode.equals(verificationCodeReceive) || !phoneNumber.equals(phoneNumberLatest)){
            ToastUtils.show(ForgetActivity.this,"验证码不正确",1);
        } else if (StringUtil.isNullOrEmpty(newPassword)){
            ToastUtils.show(ForgetActivity.this,"密码不能为空",1);
        } else if (!Boolean_Mobile.isPassWord(newPassword)) {
            ToastUtils.show(ForgetActivity.this,"密码长度应为6~15个字符，请重新输入",1);
        } else if (StringUtil.isNullOrEmpty(newPasswordConfirm)){
            ToastUtils.show(ForgetActivity.this,"请再次输入密码",1);
        } else if (!newPassword.equals(newPasswordConfirm)) {
            ToastUtils.show(ForgetActivity.this,"两次输入的密码不一致，请重新输入",1);
        } else {
            HashMap<String, String> params = new HashMap<>();
            params.put("Mobile",phoneNumber);
            params.put("Pwd",newPassword);
            params.put("NewPwd", newPasswordConfirm);

            KsoapUtil ksoapUtil = new KsoapUtil("ReSetPwd", params, new PostCallback() {
                @Override
                public void onSuccess(String result) {
                    JsonModel jsonModel = JSON.parseObject(result, JsonModel.class);
                    Log.i("JsonModel", "jsonModel" + jsonModel);
                    if (jsonModel.isSuccess()) {
                        ToastUtils.show(ForgetActivity.this,"修改成功", 1);
                        finish();
                    } else {
                        ToastUtils.show(ForgetActivity.this, jsonModel.getMessage(), 1);
                    }
                }

                @Override
                public void onFail(Exception e) {

                    ToastUtils.show(ForgetActivity.this, "访问异常", 1);
                    e.printStackTrace();
                }
            });
            ksoapUtil.execute();
        }
    }

    /**
     * 获取验证码
     */
    private void sendVerificationCode() {

        HashMap<String, String> params = new HashMap<>();
        params.put("Mobile", phoneNumber);
        params.put("Type", "FindPassword");
        KsoapUtil ksoapUtil = new KsoapUtil("SendVerCode", params, new PostCallback() {
            @Override
            public void onSuccess(String result) {
                JsonModel jsonModel = JSON.parseObject(result, JsonModel.class);
                if (jsonModel.isSuccess()) {
                    verificationCodeReceive = jsonModel.getMessage();
                }
            }
            @Override
            public void onFail(Exception e) {
                ToastUtils.show(ForgetActivity.this, "网络异常", 1);
                e.printStackTrace();
            }
        });
        ksoapUtil.execute();
    }



}
